//
//  JMVideoDetailController.m
//  PhotoLock
//
//  Created by 赵俊明 on 2017/7/15.
//  Copyright © 2017年 赵俊明. All rights reserved.
//

#import "JMVideoDetailController.h"
#import "JMDetailModel.h"
#import "JMBottomTabView.h"
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "TZImageManager.h"
#import "TZImagePickerController.h"

@interface JMVideoDetailController ()<JMBottomTabViewDelegate, TZImagePickerControllerDelegate>

@end

@implementation JMVideoDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"Videos";
    
    NSArray *imags = @[@"image", @"image", @"image", @"image",@"image", @"image", @"image", @"image",@"image", @"image", @"image", @"image",@"image", @"image", @"image", @"image"];
    
    for (int i = 0; i < imags.count; i ++) {
        
        JMDetailModel *model = [[JMDetailModel alloc] init];
        model.imageName = imags[i];
        [self.dataSource addObject:model];
    }
    
    self.collection.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-54);
    [self.collection reloadData];
    
    JMBottomTabView *bottom = [[JMBottomTabView alloc] initWithFrame:CGRectMake(0, kSCREEN_HEIGHT-54, kSCREEN_WIDTH, 54)];
    bottom.items = @[@"Add", @"Delegate", @"Export"];
    bottom.delegate = self;
    [self.view addSubview:bottom];
}

- (void)buttomTabSelection:(NSInteger)index
{
    if (index == 0) {
        
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:6 delegate:self];
        imagePickerVc.allowPickingOriginalPhoto = NO;
        imagePickerVc.allowPickingImage = NO;
        imagePickerVc.allowPickingVideo = YES;
        [self presentViewController:imagePickerVc animated:YES completion:nil];
        
    }else if (index == 1){
        
        NSLog(@"删除");
        
    }else{
     
        NSLog(@"导出");
        
    }
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideos:(NSArray *)models
{
    
//    NSMutableArray *assets = [NSMutableArray array];
//    for (TZAssetModel *model in models) {
//        
//        NSString *videoPath = [kVideos stringByAppendingPathComponent:self.folderName];
//        [[TZImageManager manager] getVideoOutputPathWithAsset:model.asset outPutPath:videoPath completion:^(NSString *outputPath) {
//            
//            NSLog(@"%@", outputPath);
//            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//                [PHAssetChangeRequest deleteAssets:@[model.asset]];
//            } completionHandler:^(BOOL success, NSError *error) {
//                NSLog(@"Error: %@", error);
//            }];
//            
//            [assets addObject:model.asset];
//        }];
//    }
    
    //    if (assets.count>0) {
    //
    //        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    //            [PHAssetChangeRequest deleteAssets:@[assets]];
    //        } completionHandler:^(BOOL success, NSError *error) {
    //            NSLog(@"Error: %@", error);
    //        }];
    //    }
    
    //    int i = 0;
    //    for (UIImage *image in photos) {
    //
    //        NSString* path = [[infos[i] objectForKey:@"PHImageFileURLKey"] absoluteString];
    //        NSData *data = UIImageJPEGRepresentation(image, 1.0);
    //        NSString *imageName;
    //        if (path) {imageName = [path lastPathComponent];
    //
    //        }else{imageName = [NSString stringWithFormat:@"%@.JPG", [JMHelper confromTimesp]];}
    //
    //        NSString *imageFolderDir = [[kPhotos stringByAppendingPathComponent:self.folderName] stringByAppendingPathComponent:imageName];
    //        [data writeToFile:imageFolderDir atomically:YES];
    //        i ++;
    //    }
    //
    //    [MobClick event:@"FiveStar_results_show"];
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //
    //        DeleteDoneController *done = [[DeleteDoneController alloc] init];
    //        JMMainNavController *navC = [[JMMainNavController alloc] initWithRootViewController:done];
    //        [self presentViewController:navC animated:YES completion:nil];
    //    });
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JMDetailCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionDetailID forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark UICollectionViewDelegate
// 选中某item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JMDetailModel *model = self.dataSource[indexPath.row];
    NSURL *URL = [NSURL fileURLWithPath:model.imageName];
    MPMoviePlayerViewController  * moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:URL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayerController];
    moviePlayerController.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
}

// 动态设置每个分区的EdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

// 动态设置每行的间距大小
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kSCREEN_WIDTH-40)/3, (kSCREEN_WIDTH-40)/3);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
